package platform.database;

import java.util.*;

public class Database {
    private String appName;
    private Map<String, Table> tables;

    public Database() {
        tables = new HashMap<>();
    }

    public Database(String appName) {
        this.appName = appName;
        tables = new HashMap<>();
    }

    public List<String> dbHeader(String tableName) {
        if (!tables.containsKey(tableName)) {
            return new ArrayList<>(); // 表不存在
        }

        Table table = tables.get(tableName);
        return table.header();
    }

    public  boolean dbCreate(String tableName, String primaryKey, List<String> header, int rowLimit) {
        if (tables.containsKey(tableName) || rowLimit < -1) {
            return false; // 表已存在或行数限制不合法
        }

        Table table = new Table(tableName, primaryKey, header, rowLimit);
        tables.put(tableName, table);
        return true;
    }

    public  boolean dbDrop(String tableName) {
        if (!tables.containsKey(tableName)) {
            return false; // 表不存在
        }

        tables.remove(tableName);
        return true;
    }

    public  boolean dbInsert(String tableName, Map<String, Object> row) {
        if (!tables.containsKey(tableName)) {
            return false; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.insert(row);
    }

    public  boolean dbUpdate(String tableName, Map<String, Object> row) {
        if (!tables.containsKey(tableName)) {
            return false; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.update(row);
    }

    public  boolean dbDelete(String tableName, int row,
                     List<Condition> condition, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return false; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.delete(row, condition, orders);
    }

    public boolean dbDeletes(String tableName, int rows,
                             List<Condition> condition, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return false; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.deletes(rows, condition, orders);
    }

    public Map<String, Object> dbRow(String tableName, int row, List<String> columns,
                                     List<Condition> condition, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return new HashMap<>(); // 表不存在
        }

        Table table = tables.get(tableName);
        return table.row(row, columns, condition, orders);
    }

    public List<Map<String, Object>> dbRows(String tableName, int rows, List<String> columns,
                                             List<Condition> condition, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return new ArrayList<>(); // 表不存在
        }

        Table table = tables.get(tableName);
        return table.rows(rows, columns, condition, orders);
    }

    public List<Object> dbColumn(String tableName, int rows, String column,
                                 List<Condition> conditions, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return new ArrayList<>(); // 表不存在
        }

        Table table = tables.get(tableName);
        return table.column(rows, column, conditions, orders);
    }

    public Map<String, List<Object>> dbColumns(String tableName, int rows, List<String> columns,
                                               List<Condition> conditions, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return new HashMap<>(); // 表不存在
        }

        Table table = tables.get(tableName);
        return table.columns(rows, columns, conditions, orders);
    }

    public Object dbCell(String tableName, int row, String column,
                         List<Condition> conditions, List<Order> orders) {
        if (!tables.containsKey(tableName)) {
            return "@#$%"; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.cell(row, column, conditions, orders);
    }

    public int dbCount(String tableName) {
        if (!tables.containsKey(tableName)) {
            return -1; // 表不存在
        }

        Table table = tables.get(tableName);
        return table.count();
    }
}
